import { Alert, DeviceEventEmitter } from 'react-native'
import { isProd } from '../../src/configs/api'
import ajaxAsync from '../../src/utils/ajaxAsync'
import { LogUtils } from 'rn-xiaobu-utils'
import NavigationService from '../../src/utils/NavigationService'

/**
  * @description: 这是一个订单所有操作方法的工具类
  * @author: yingying.fan
  * @date: 2019/3/6 9:44
  */

// 订单操作事件名称
const OrderOptionEvent = 'OrderOptionEvent'
// 订单操作事件类型（需要改变界面或者路由）
const OrderOptionTypes = {
  RECEIPT: 'order_op_receipt',
  CLOSE: 'order_op_close',
  DELETE: 'order_op_delete'
}

/**
 *
 * @param title 弹框标题
 * @param msg 弹框提示
 * @param okPress 确认回调
 * @param cancelPress 取消回调 默认空函数
 * @param buttons 按钮文字 默认 ['取消', '确定']
 */
function showConfirmDialog (title, msg, okPress, cancelPress = () => {
}, buttons = ['取消', '确定']) {
  LogUtils.log('buttons length: ' + buttons.length)
  let btnList = [{
    text: buttons[0],
    onPress: cancelPress
  }, {
    text: buttons[1],
    onPress: okPress
  }]
  Alert.alert(title, msg, btnList, { cancelable: false })
}

/**
 * 发送订单操作是事件
 * @param type 事件类型
 * @param info 具体信息
 */
function fireOrderOptionEvent (type, info) {
  DeviceEventEmitter.emit(OrderOptionEvent, {
    type: type,
    info: info
  })
}

function confirmOrderOption (msg, postUrl, postInfo, eventType, eventInfo) {
  showConfirmDialog('提示', msg, () => {
    if (isProd) {
      ajaxAsync(postUrl, postInfo)
        .then(() => {
          // 发送事件，让订单列表和详情页面自行处理
          fireOrderOptionEvent(eventType, eventInfo)
        })
        .catch(() => {
        })
    } else {
      fireOrderOptionEvent(eventType, eventInfo)
    }
  })
}

const OrderOptionFuncs = {
  // 1 支付订单
  payOrder: (orderInfo, orderIndex) => {

  },
  // 2 关闭订单
  closeOrder: (orderInfo, orderIndex) => {
    let orderId = orderInfo.ORDER_ID
    confirmOrderOption(
      '确定关闭订单?',
      '/order/close',
      { ORDER_ID: orderId },
      OrderOptionTypes.CLOSE,
      { ORDER_ID: orderId, INDEX: orderIndex }
    )
  },
  // 3 确认收货
  receiptOrder: (orderInfo, orderIndex) => {
    let orderId = orderInfo.ORDER_ID
    confirmOrderOption(
      '确定收货?',
      '/order/receipt',
      { ORDER_ID: orderId },
      OrderOptionTypes.RECEIPT,
      { ORDER_ID: orderId, INDEX: orderIndex }
    )
  },
  // 4 评价
  commentOrder: (orderInfo, orderIndex) => {
    let orderId = orderInfo.ORDER_ID
    NavigationService.push('Comment', { ORDER_ID: orderId })
  },
  // 5 查看物流
  searchLogistics: (orderInfo, orderIndex) => {

  },
  // 6 退款申请
  refundApplicate: (orderInfo, orderIndex) => {

  },
  // 7 售后
  afterSaleService: (orderInfo, orderIndex) => {

  },
  // 8 退款中，查看退款
  refunding: (orderInfo, orderIndex) => {

  },
  // 9 退货
  refundGoods: (orderInfo, orderIndex) => {

  },
  // 10 取消退款申请
  closeRefund: (orderInfo, orderIndex) => {

  },
  // 11 修改退款申请
  modifyRefund: (orderInfo, orderIndex) => {

  },
  // 14 查看卡券的页面
  checkTickets: (orderInfo, orderIndex) => {

  },
  // 15 删除订单
  removeOrder: (orderInfo, orderIndex) => {
    let orderId = orderInfo.ORDER_ID
    confirmOrderOption(
      '确认删除订单?',
      '/order/delete',
      { ORDER_ID: orderId },
      OrderOptionTypes.DELETE,
      { ORDER_ID: orderId, INDEX: orderIndex }
    )
  },
  // 16 每日试评论
  commentDailyTrial: (orderInfo, orderIndex) => {

  }
}

export {
  OrderOptionEvent,
  OrderOptionTypes,
  OrderOptionFuncs
}